package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-08-30
 * Time: 11:01
 */
//两把锁首先获取自己的锁，之后尝试获取对方的锁

public class ThreadDemo14 {
    public static void main(String[] args) {
        Object jiangyou = new Object();
        Object cu = new Object();
        Thread t1 = new Thread(() ->{
            synchronized (jiangyou){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (cu){
                    System.out.println("su获取");
                }
            }

        });
        Thread zheng = new Thread(() ->{
            synchronized (cu){

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (jiangyou){
                    System.out.println("郑获取");
                }
            }

        });
        t1.start();
        zheng.start();
    }
}
